<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<html>
<head>
<meta HTTP-EQUIV="pragma" CONTENT="no-cache">
<meta HTTP-EQUIV="Cache-Control" CONTENT="no-cache, must-revalidate">
<meta HTTP-EQUIV="expires" CONTENT="0">
<%
response.setHeader("Cache-Control","no-store");
response.setHeader("Pragrma","no-cache");
response.setDateHeader("Expires",0);
%>
<title>快递交接</title>
<script type='text/javascript'
      src='http://wms.ttyuncang.com/WebSpeech.js'></script>
<script type="text/javascript">
  var method="";
  var sendCodeTemp = "";
  var curExpressCompany = "";
  $(function() {
	  //if("".equals($("#_warehouseCode"))){
	if(""==$("#_warehouseCode").val()){
		 $("#_warehouseCode").focus();
	  }else{
	  	$("#txt_send").focus();
	  } 
  });
  
  //快递公司值改变
  	function selectCurrentCompanyCode() {
  		 if (curExpressCompany == ""){
		} else {		    
			var expressCompany=$("#_expressCompany").find("option:selected").text();
			if(curExpressCompany == expressCompany) {
			}else {
				jAlert("当前选择的快递公司与当前流水号中快递公司不一致,完成本次扫描。","提示");
				refresh();
				return;
			}
		} 
	}
  
  //发货
  function sendout(){
  
	  showBg();
	  
	  var logistic=$("#txt_send").val();
	  var warehouse=document.getElementById("_warehouseCode");
	  var warehouseCode=warehouse.options[warehouse.selectedIndex].value;
	  var express=document.getElementById("_expressCompany");
	  var expressCompany=express.options[express.selectedIndex].value;
	  if(logistic==''){
		  $("#txt_send").select();
		  $("#txt_send").focus();
		  $("#msg_send").html("请录入快递单号!");
	  }
	  if(method=='') method="send";
	  $.ajax({
			type:'post',
			url:'${ctx}/sendout/'+ method,
			data:{
				logistic:logistic,
				warehouseCode:warehouseCode,
				expressCompany:expressCompany,
				sendCodeTemp : sendCodeTemp
			},
			dataType:'json',
			success:function(data) {
				var msg=data.msg;
				
				if(msg=='success'){
					var sendout=data.sendOut;
					var sendtime = data.sendTime;
					var expressCompany = data.expressCompany;
					var warehouse = data.warehouse;
					
					//当前快递公司
					 curExpressCompany = data.expressCompany;
					
					//当前流水号
					sendCodeTemp = sendout.sendCode;

				    showSendOut(sendout,sendtime,expressCompany,warehouse);
				    
				    $("#txt_send").val('');
					$("#txt_send").focus();
				    
					$("#msg_send").css("color","green");
					$("#msg_send").html("成功");
					WebSpeech.speak("成功");
				}
				else if(msg == "repeat"){
					WebSpeech.speak("快递单号重复扫描");
					alert("快递单号重复扫描");
					$("#txt_send").val('');
					$("#txt_send").focus();
					$("#msg_send").css("color","red");
					$("#msg_send").html("快递单号重复扫描");
				} else if(msg == "expressCompany error"){
					WebSpeech.speak("快递公司与快递单号不符");
					alert("快递公司与快递单号不符");
					$("#txt_send").val('');
					$("#txt_send").focus();
					$("#msg_send").css("color","red");
					$("#msg_send").html("快递公司与快递单号不符");
				} else if(msg == "work error"){
					WebSpeech.speak("找不到快递单号对应的订单");
					alert("找不到快递单号对应的订单");
					$("#txt_send").val('');
					$("#txt_send").focus();
					$("#msg_send").css("color","red");
					$("#msg_send").html("找不到快递单号对应的订单");
				} else if(msg == "warehouse error"){
					WebSpeech.speak("快递单号与仓库不对应");
					alert("快递单号与仓库不对应");
					$("#txt_send").val('');
					$("#txt_send").focus();
					$("#msg_send").css("color","red");
					$("#msg_send").html("快递单号与仓库不对应");
				}else if(msg == "excetpion error"){
					alert(data.content);
					$("#txt_send").val('');
					$("#txt_send").focus();
					$("#msg_send").css("color","red");
					$("#msg_send").html("失败");
				}
				else
				{
					WebSpeech.speak("失败");
					alert("快递交接失败");
					$("#txt_send").val('');
					$("#txt_send").focus();
					$("#msg_send").css("color","red");
					$("#msg_send").html("失败");
				}
				
				closeBg();
			},
			error:function() {
				WebSpeech.speak("失败");
				alert("网络繁忙，请稍后重试...");
				
				closeBg();
			}
	   });
	  
	
  }
  
  function showSendOut(sendout,sendtime,expressCompany,warehouse){
	  var str = "<tr> <td>"+sendout.sendCode+"</td>";
	  str += "<td>"+sendout.logisticNumber+"</td>";
	  str += "<td>"+expressCompany+"</td>"
      str += "<td>"+sendtime+"</td>";
      str += "</tr>";
      $("#last_send_id").html(str);
      
      /* for(var sendOutSum in sendOutList){
    	  var str2 = "<tr><td>" + sendOutSum.customer + "</td>";
    	  str2 += "<td>" + sendOutSum.warehouse + "</td>";
    	  str2 += "<td>" + sendOutSum.company + "</td>";
    	  str2 += "<td>" + sendOutList[sendOutSum] + "</td>";
    	  str2 += "</tr>";
      } */
      var table = document.getElementById("contentTable2");
      var flag = "N";
      var trId = expressCompany + "_" + warehouse;
      for(var i = 0; i < table.rows.length; i++){
		    if(table.rows[i].id == trId){
			 table.rows[i].cells[3].innerText = parseInt(table.rows[i].cells[3].innerText) + 1; 
		  	 flag = "Y";
		  	 break;
	      } 
	} 
	   if(flag == "N"){
		   var str2 = "<tr id="+ "\"" + trId + "\""+ ">" ;
		   str2 += "<td>" + sendout.sendCode + "</td>";
		  str2 += "<td>" + expressCompany + "</td>";
    	  str2 += "<td>" + warehouse + "</td>";
    	  str2 += "<td>" + "1" + "</td>";
    	  str2 += "<td><a class=\"btn btn-primary\" onclick=\"finish();\">完成</a>"; /* onclick =initPrint('"+sendout.sendCode+"'') */
    	  str2 += "<a class=\"btn btn-primary\" href='#printExpressInfo' data-toggle='modal' onclick =initPrint('"+sendout.sendCode+"','"+expressCompany+"','"+warehouse+"')>完成并打印</a> </td></tr>";
          $("#send_list_id").append(str2); 
	  }  
   }
  
  
  //完成
  function finish(){
	  refresh();
  }
  
 function initPrint(sendCode,expressCompany,warehouse){
	  packFormDefaultPrinter("expressPrint"); 	
	  $("#printSendCode").val(sendCode);
	  $("#printExpressCompany").val(expressCompany);
	  $("#printWarehouse").val(warehouse);
  }
 
  //完成并打印
  function printStyle(){
	  showBg();
	  $.ajax({
	  		type : 'post',
	  		url : '${ctx}/sendout/ajaxPrintExpressList',
	  		data : 'sendCode=' +  $("#printSendCode").val(),
	  		dataType : 'json',
	  		success : function(data) {
	  			 if (data.status == "1") {
	  				initLodop();
		  			printExpressListStyle(data.content);
	  				refresh();
	  			} else if (data.status == "0") {
	  		    	jAlert(data.content,"错误提示");
	  			} 
	  			 closeBg();
	  		},

	  		error : function() {
	  			 closeBg();
	             alert("服务器异常");
	  		}
	  	});
  }
  
  function printExpressListStyle(sendOut){
	  LODOP.PRINT_INIT($("#printSendCode").val());
	  var headStr ="<table width='100%' cellpadding='0' cellspacing='0' align = 'center' border='1'>";
	  headStr +="<thead>"
	  headStr +="<tr><td colspan= '8' align = 'center'><h3>快递交接清单</h3></td></tr>";
	  headStr +="<tr><td>流水号</td><td colspan= '3'>"+$("#printSendCode").val()+"</td><td>快递公司</td><td colspan= '3'>"+ $("#printExpressCompany").val()+"</td></tr>";
	  headStr +="<tr><td>仓库</td><td colspan= '3'>"+$("#printWarehouse").val()+"</td><td>打印时间</td><td colspan= '3'>"+new Date().Format("yyyy-MM-dd hh:mm:ss")+"</td></tr>";
	  headStr += "<tr style='font-size:10pt'><td>快递单号</td><td  colspan= '7'></td></tr>";
	  headStr +="</thead>";
      var rows = sendOut.length  % 4 == 0 ?   Math.floor(sendOut.length / 4 ) :  Math.ceil(sendOut.length / 4 );
      var m = 0;
      for (var i = 1 ; i <= rows ; i++){
    	  headStr +="<tr style='font-size:12pt' >";
	    for(var j =1;j <= 4; j++){
	    	if( m < sendOut.length){
	    		headStr +="<td  colspan= '2'  width='25%'>"+sendOut[m].logisticNumber+"</td>";
	    		m++;
	    	}
	    	else{
	    		break;
	    	}
	    }
	    headStr +="</tr>";
     }
  	headStr += "<tr><td colspan= '6'></td><td colspan= '2' align = 'left'>合计:"+sendOut.length+"</td></tr>";
	headStr += "<tr><td  align = 'left'>仓库签字</td><td   colspan= '3'></td><td  align = 'left'>快递签字</td><td   colspan= '3'></td></tr>";
	headStr += "<tr><td  align = 'left'>时间</td><td   colspan= '3'></td><td  align = 'left'>时间</td><td   colspan= '3'></td></tr>";
      headStr += "</table>";
	 LODOP.ADD_PRINT_TABLE(50,0,"100%","85%",headStr);
	 LODOP.SET_PRINT_PAGESIZE(1, 0, 0, "A4") ;
	 if (LODOP.SET_PRINTER_INDEXA($("#expressPrint").val())) {
			LODOP.PRINT(); 
	 }
  }
  
  function refresh(){
	  $("#txt_send").val('');
	  $("#txt_send").focus();
	  $("#last_send_id").html("");
      $("#send_list_id").html("");
      sendCodeTemp = "";
      curExpressCompany = "";
      $("#printSendCode").val("");
	  $("#printExpressCompany").val("");
	  $("#printWarehouse").val("");
 }
  //杭州仓的可以根据自己维护的快递号段进行操作，其他仓快递交接的数据必须是wms系统订单存在的
  function choiceMode(){
	 var warehouseCode=$("#_warehouseCode").val();
	 $("#msg_send").html("");
	 if(warehouseCode=='HZTT'){
		 method="send";
		 document.getElementById("expressCompanyDiv").style.display="";//显示
	 }else{
		 method="send2";
		 document.getElementById("expressCompanyDiv").style.display="none";//隐藏
	 }
}
  
  function initLodop() {
		LODOP = getLodop(document.getElementById('LODOP_OB'), document.getElementById('LODOP_EM'));
 }
  
</script>
</head>

<body>

<div id="fullbg"></div>
<div id="dialog">
	<div>
		<img src="${ctx }/static/022.gif"></img>
	</div>
</div>
<section class="content">
	<div class="row"></div>
	<div class="col-md-12">
			<div class="box box-solid box-primary" >
				<div class="box-header">
					<i class="fa fa-search"></i>
					<h3 class="box-title">快递交接</h3>
					<div class="box-tools pull-right">
						<button class="btn btn-primary btn-sm" data-widget="collapse">
							<i class="fa fa-minus"></i>
						</button>
					</div>
				</div>
				<div class="box-body">
					<div class="form-group row">
						<div class="col-sm-3">
						<div class="input-textgroup col-sm-12">
							<span class="input-group-text">仓库：</span>
							<select  name="_warehouseCode"  id="_warehouseCode" class="form-control" onchange="choiceMode()">
							<option value=""></option>
							<c:forEach items="${warehouseList}" var="w">
						   		<option value="${w.groupCode}" 
						   		<c:if test="${param._warehouseCode eq w.groupCode }">selected=selected</c:if>
						   		>${w.groupName}</option>
					   		</c:forEach>
							</select>
						</div>
						</div>
					</div>
					
					<div class="form-group row" id="expressCompanyDiv" style="display:none;">
						<div class="col-sm-3">
						<div class="input-textgroup col-sm-12">
					<span class="input-group-text">快递公司：</span>
					<select  name="_expressCompany"  id="_expressCompany" class="form-control"  onchange = "selectCurrentCompanyCode();">
						<option value=""></option>
						<c:forEach items="${cmpList}" var="w">
						   <option value="${w.companyCode}" 
						   <c:if test="${param._expressCompany eq w.companyCode }">selected=selected</c:if>
						   >${w.companyName}</option>
					   </c:forEach>
					</select>
						</div>
						</div>
					</div>
			
					<div class="form-group row">
		     		<div class="col-sm-3">
					<div class="input-textgroup col-sm-12">
		   		 		<span class="input-group-text">   快递单号：</span>
		   		  		<input type="text" class="form-control" id="txt_send" onchange="sendout();" />
		   				<span style="color: red;font-weight: bold;" id="msg_send"></span>
		   			</div>
					</div>
					</div>
				</div>
			</div>
	</div>	
	<div class="form-group row">
	   <div class="col-md-12">
	      <div class="box box-solid box-primary" >
	         <div class="box-header">
				<i class="fa fa-search"></i>
			    <h3 class="box-title">扫描结果</h3>
				<div class="box-tools pull-right">
				  <button class="btn btn-primary btn-sm" data-widget="collapse">
						<i class="fa fa-minus"></i>
				  </button>
				</div>
			 </div>
			 
			 <div class="box-body">
				<table id="contentTable" class="table table-striped table-bordered table-condensed">
				<thead>
				<tr>
				    <th>装车流水号</th>
					<th>快递单号</th>
					<th>快递公司</th>
					<th>交接时间</th>
			   </tr>
			   </thead>
			<tbody id="last_send_id">
			</tbody>
	</table>
	</div>
	      </div>
	   </div>
	</div>	 
	<div class="form-group row">
		<div class="col-md-12">
			<div class="box box-solid box-primary">
				<div class="box-header">
					<i class="fa fa-list"></i>
					<h3 class="box-title">当日快递交接结果</h3>
					<div class="box-tools pull-right">
						<button class="btn btn-primary btn-sm" data-widget="collapse" type="button">
							<i class="fa fa-minus"></i>
						</button>
					</div>
				</div>
	<div class="box-body">
	<table id="contentTable2" class="table table-striped table-bordered table-condensed">
		<thead>
		<tr>
		    <th>装车流水号</th>
			<th>快递公司</th>
			<th>仓库</th>
			<th>快递单量</th>
		</tr>
		</thead>
		<tbody id="send_list_id">
		</tbody>
	</table>
	</div>
	</div>
	</div>
	</div>
	
	<!-- 打印界面 -->
	<div class="modal  fade"tabindex="-1" role="dialog" aria-labelledby="myModalLabel"	aria-hidden="true" id="printExpressInfo">
			<div class="modal-dialog">
				<div class="modal-content">
					<div class="modal-header">
						<h3 id="myModalLabel">
		                                                        打印快递交接信息
						</h3>
						<span id="messageLable"></span>
						<input type="hidden" id="printSendCode"/>
						<input type="hidden" id="printExpressCompany"/>
						<input type="hidden" id="printWarehouse"/>
					</div>
					<div class="modal-body" style="overflow: auto;">
						<div class="form-group row">
							<div class="col-sm-9">
								<div class="input-group col-sm-12">
									<span class="input-group-text"> 选择打印机: </span> 
									<select id="expressPrint" name="expressPrint" class="form-control"  >
			
									</select>
								</div>
							</div>
						</div>
					</div>
					
		               <div class="modal-footer">
						<a class="btn btn-primary" data-dismiss="modal" aria-hidden="true" onclick="printStyle()"> 立即打印 </a>
						<a class="btn" data-dismiss="modal" aria-hidden="true"> 关闭 </a>
					  </div>
		  	</div>
		  </div>
		</div>
</section>
</body>
</html>
